<template>
  <el-dialog title="定薪" :visible="showDialogFixed" @close="btnCancel">
    <div class="fixedDalary">
      <el-form label-width="120px" :model="formData" style="padding:0 auto">
        <el-form-item label="当前基本工资">
          <el-input
            v-model="formData.currentBasicSalary"
            placeholder="当前基本工资"
            style="width: 60%"
            type="number"
          />
        </el-form-item>
        <el-form-item label="当前岗位工资">
          <el-input v-model="formData.currentPostWage" placeholder="当前岗位工资" style="width: 60%" type="number" />
        </el-form-item>
        <el-form-item label="当前工资合计">
          <el-input v-model="computeCurrentTotal" placeholder="当前工资合计, 自动计算" style="width: 60%" :disabled="true" />
        </el-form-item>
        <el-form-item label="转正基本工资">
          <el-input
            v-model="formData.correctionOfBasicWages"
            placeholder="转正基本工资"
            style="width: 60%"
            type="number"
          />
        </el-form-item>
        <el-form-item label="转正岗位工资">
          <el-input v-model="formData.turnToPostWages" placeholder="转正岗位工资" style="width: 60%" type="number" />
        </el-form-item>
        <el-form-item label="转正工资合计">
          <el-input v-model="computeTurnTotal" placeholder="当前基本工资, 自动计算" style="width: 60%" :disabled="true" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="btnOK">确认</el-button>
          <el-button @click="btnCancel">关闭</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import { fixedSalary } from '@/api/salarys'
export default {
  props: {
    showDialogFixed: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      formData: {
        currentBasicSalary: '',
        currentPostWage: '',
        correctionOfBasicWages: '',
        turnToPostWages: ''
      }
    }
  },
  computed: {
    // 得到在输入框中输入的当前基本工资和当前岗位工资的和
    computeCurrentTotal() {
      let base = 0
      if (this.formData.currentBasicSalary != null) {
        base = Number(this.formData.currentBasicSalary)
      }
      let post = 0
      if (this.formData.currentPostWage != null) {
        post = Number(this.formData.currentPostWage)
      }
      return base + post
    },
    // 得到转正基本工资和转正岗位工资的和
    computeTurnTotal() {
      let base = 0
      if (this.formData.correctionOfBasicWages != null) {
        base = Number(this.formData.correctionOfBasicWages)
      }
      let post = 0
      if (this.formData.turnToPostWages != null) {
        post = Number(this.formData.turnToPostWages)
      }
      return base + post
    }
  },
  methods: {
    btnCancel() {
      // 表单清空
      this.formData = {
        currentBasicSalary: '',
        currentPostWage: '',
        correctionOfBasicWages: '',
        turnToPostWages: ''
      }
      // 关闭弹窗
      this.$emit('update:showDialogFixed', false)
    },
    async btnOK() {
      // 发请求
      await fixedSalary({ ...this.formData, userId: this.userId })
      // 提示
      this.$message.success('定薪成功')
      // 提示父组件刷新数据
      this.$emit('success')
      // 关闭弹窗
      this.$emit('update:showDialogFixed', false)
    }
  }
}
</script>

<style  lang="scss" scoped>

  ::v-deep .el-form-item {
      margin: 15px auto;
  }

</style>
